using Godot;
using System;

public partial class BulletTime : Node
{
	public static BulletTime Instance {get;private set;}
	public Player player;
	public Enemy enemy;							

	public CanvasModulate canvasModulate;

	public float intensity = 3.0f;

	public TimeController timeController;
	
	//播放时间
	public Timer CD;
	
	public bool IsCDReady = true;

	[Export]
	public float ScaleEnemy=0.5f,
		ScalePlayer=0.9f;

    public override void _Ready()
    {
		Instance = this;		
        
		player=GetNode<Player>("/root/Main/Player");
		enemy=GetNode<Enemy>("/root/Main/Enemy");				
				
		canvasModulate = GetNode<CanvasModulate>("CanvasModulate");
		canvasModulate.Visible = false;		

		CD=GetNode<Timer>("CD");
		CD.Timeout+=OnCDTimeout;

		timeController = TimeController.Instance;
    }

	public void OnCDTimeout()
	{		
		IsCDReady = true;
		
		timeController.SetTimeScale(player,1.0f);
		timeController.SetTimeScale(enemy,1.0f);

		player.Modulate = player.OriginColor;
		player.CurrentColor=player.Modulate;

		canvasModulate.Visible=false;

		foreach(AnimatedSprite2D anima in MyUtil.GetAnimatedSprite2Ds(player))
		{
			anima.SpeedScale = 1.0f;
		}		

		foreach(AnimatedSprite2D anima in MyUtil.GetAnimatedSprite2Ds(enemy))
		{
			anima.SpeedScale = 1.0f;
		}
	}
    public void UseBulletTime()
	{	
		if(IsCDReady)
		{
			IsCDReady=false;
			GD.Print("进入子弹时间");
			timeController.SetTimeScale(player,ScalePlayer);
			timeController.SetTimeScale(enemy,ScaleEnemy);						
			
			GetNode<AudioStreamPlayer>("Music").Play();

			player.Modulate =player.OriginColor*intensity;
			player.CurrentColor=player.Modulate;

			canvasModulate.Visible = true;	

			foreach(AnimatedSprite2D anima in MyUtil.GetAnimatedSprite2Ds(player))
			{
				anima.SpeedScale = ScalePlayer;
			}
									
			foreach(AnimatedSprite2D anima in MyUtil.GetAnimatedSprite2Ds(enemy))
			{
				anima.SpeedScale = ScaleEnemy;
			}

			CD.Start();
		}
	}
}
